#include <iostream>
#include <vector>

#define STB_IMAGE_IMPLEMENTATION

#include <GL.h>

using namespace GLM;

float alpha = 0;
float deltaTime = 0.1;
bool firstMouse = true;
double lastX, lastY;

Camera camera(Vector4(10, 0, 0, 1.0));
Texture texture;
Texture texture1;
Texture texture2;
Cube cube;
Cube cube1;
Sphere sphere;
DWORD startTime = GetTickCount();

void draw(GLWindow &window) {

    Graphic graphic = window.createGraphic();
    if (window.getKeyState(VK_UP)) camera.ProcessKeyboard(FORWARD, deltaTime);
    if (window.getKeyState(VK_DOWN)) camera.ProcessKeyboard(BACKWARD, deltaTime);
    if (window.getKeyState(VK_LEFT))camera.ProcessKeyboard(LEFT, deltaTime);
    if (window.getKeyState(VK_RIGHT)) camera.ProcessKeyboard(RIGHT, deltaTime);

//    Matrix4f m;
    Vector4 v(-1, -0.5, 1, 1.0);
//    rotate(m,alpha,v);
//    graphic.loadMatrix(m);
//    graphic.VertexPointer(&mesh);
//    graphic.drawElement(&indices);
    graphic.loadTexture(texture1);
    Matrix4f view = camera.getViewMatrix();
    window.setView(view);
    Vector4 tv(0.0, 1.5, 0, 1.0);
    cube.m.setIdentity();

    cube.rotate(alpha, v);
    cube.translate(tv);
    cube.draw(graphic);
    graphic.loadTexture(texture2);
    cube1.m.setIdentity();
    cube1.rotate(alpha, v);
    Vector4 tv2(0.0, -1.5, 0, 1.0);
    cube1.scale(Vector4(0.5, 0.5, 0.5, 1.0));


    cube1.translate(tv2);
    cube1.rotate(alpha, Vector4(0, 0, 1, 1));

//
//
////    cube1.translate(tv2);
//
    cube1.draw(graphic);
//
    Vector4 v2(0.0, 0.0, 2.0, 1.0);
    graphic.loadTexture(texture);
    sphere.m.setIdentity();
    sphere.rotate(alpha, v2);
    Vector4 tv3(2, -3.5, 0, 1.0);
    sphere.scale(Vector4(2.0f, 2.0f, 2.0f, 1.0f));
    sphere.translate(tv3);
    sphere.draw(graphic);
    DWORD now = GetTickCount();
    float delta = 1.0f * (now - startTime) / 1000 * PI/2;
    alpha = delta;

}

void mouse_callback(double xpos, double ypos) {
    //如果为第一次进入 记录当前鼠标位置 为初始化鼠标位置
    if (firstMouse) {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }
    //计算当前帧和上一帧的位置差
    double xoffset = xpos - lastX;
    double yoffset = lastY - ypos; // 这里如果不反过来 实际操作过程中向上移动摄像机 物体也会跟着向上移动 实际应该向下
    lastX = xpos;
    lastY = ypos;
    camera.ProcessMouseMovement(-yoffset, -xoffset);
}

int main() {
    Matrix4f m = {{1, 2, 3, 4},
                  {5, 6, 7, 8},
                  {1, 2, 3, 4},
                  {5, 6, 7, 8}};
    Matrix4f m1 = {{1, 1, 1, 1},
                   {2, 2, 2, 2},
                   {3, 3, 3, 3},
                   {4, 4, 4, 4}};
    Matrix4f m2 = m + m1;
    Matrix4f m3 = m * m1;
    m = {{1, 1, 1, 1},
         {2, 2, 2, 2},
         {3, 3, 3, 3},
         {4, 4, 4, 4}};
    Vector4 v(1.0, 2.0, 3.0, 1.0);
    Vector4 v1(1, 1, 1, 1);
    Vector4 v2 = v.cross(v1);
    Matrix4f trans;
    GLM::translate(trans, v1);
    GLWindow window;


    texture.loadFormFile("texture/earth1.png");
    texture1.loadFormFile("texture/texture.png");
    texture2.loadFormFile("texture/texture2.png");
    TCHAR *title = _T("Soft rendering")
                   _T("Left/Right/Up/Down: move, Space: switch state");
    window.create(1280, 720, title);

    window.onDraw = draw;
    window.onMouseMove = mouse_callback;
    window.loop();
    return 0;


}